package org.iteam.xlz.core.service.ad;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.ad.TerminalAd;
import org.iteam.xlz.core.entity.base.Area;
import org.iteam.xlz.core.entity.images.Images;

public class TerminalAdService extends BaseService<TerminalAd>{
	
	static TerminalAdService instance;
	private List<Object> params=new ArrayList<Object>();
	private StringBuilder sql=null;
	
	private TerminalAdService(){}
	
	public static TerminalAdService getInstance(){
		if(null==instance){
			instance=new TerminalAdService();
		}
		return instance;
	}
	
	public long counts(){
		params.clear();
		sql=new StringBuilder("select count(1) from terminalad where 1=1");
		try {
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<TerminalAd> query(long page,long rows){
		params.clear();
		sql=new StringBuilder("select * from terminalad where 1=1");
		sql.append(" limit ?,?");
		params.add((page-1)*rows);
		params.add(rows);
		try {
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<TerminalAd>();
		}
	}
	
	public boolean create(TerminalAd model,String places){
		model.setAdid(UUID.randomUUID().toString().replace("-","").toUpperCase());
		model.setCreatetime(DateUtil.currentTime());
		SqlBuilder builder=SqlBuilder.createInsert("terminalad");
		builder.addField("adid",model.getAdid());
		builder.addField("begintime",model.getBegintime());
		builder.addField("endtime",model.getEndtime());
		builder.addField("status",model.getStatus());
		builder.addField("createtime",model.getCreatetime());
		builder.addField("creator",model.getCreator());
		
		String[] array=places.split("<>");
		try {
			for(String s:array){
				String sql="insert into terminaladarea(aid,adid) values(?,?)";
				super.updates(sql,s,model.getAdid());
			}
			return super.updates(builder.getSql(),builder.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(TerminalAd model,String places){
		SqlBuilder builder=SqlBuilder.createUpdate("terminalad");
		builder.addField("begintime",model.getBegintime());
		builder.addField("endtime",model.getEndtime());
		builder.addField("status",model.getStatus());
		builder.addWhere("adid",model.getAdid());
		
		String[] array=places.split("<>");
		
		try {
			super.updates("delete from terminaladarea where 1=1 and adid=?",model.getAdid());
			for(String s:array){
				String sql="insert into terminaladarea(aid,adid) values(?,?)";
				super.updates(sql,s,model.getAdid());
			}
			return super.updates(builder.getSql(),builder.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public TerminalAd findById(String id){
		try {
			return super.show("select * from terminalad where 1=1 and adid=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
			return new TerminalAd();
		}
	}
	
	public List<Area> getAreaPlaceByAdid(String adid){
		try {
			return super.query(Area.class,"select r.aid,r.name from terminaladarea l left join area r on l.aid=r.aid where l.adid=?",adid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Area>();
		}
	}
	
	public boolean delete(String adid){
		Connection con=null;
		try {
			Images img=super.show(Images.class,"select * from images where 1=1 and objectid=?",adid);
			if(null!=img){
				con=super.getConnection();
				con.setAutoCommit(false);
				int k=super.updates(con,"insert into imagesdelete(path) values(?)",img.getImgaddr());
				int j=super.updates(con,"delete from images where imgid=?",img.getImgid());
				int i=super.updates(con,"delete from terminalad where adid=?",adid);
				if(i==1&&j==1&&k==1){
					super.commit(con);
					return true;
				}else{
					super.rollback(con);
					return false;
				}
			}else{
				return super.updates(con,"delete from terminalad where adid=?",adid)>0;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
}
